package com.uxwine.alarm.alert;

import com.uxwine.alarm.Alarm;
import com.uxwine.alarm.AlarmCore;
import com.uxwine.alarm.Cmd;
import com.uxwine.alarm.Cmd.Alert;
import com.uxwine.util.Log;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Parcel;

public class AlarmReceiver extends BroadcastReceiver
{

	/**
	 * If the alarm is older than STALE_WINDOW, ignore. It is probably the
	 * result of a time or timezone change
	 */
	private final static int STALE_WINDOW = 30 * 60 * 1000;

	@Override
	public void onReceive(Context context, Intent intent)
	{
		String action = intent.getAction();
		
		if (Cmd.ACTION_ALARM_KILLED.equals(action))
		{
	    	Log.d("new-zzm", "AlarmReceiver.ACTION_ALARM_ALERT in onReceive of ACTION_ALARM_KILLED...");
	    	Log.f("snooze", "AlarmReceiver.onReceive: ACTION_ALARM_KILLED...");
			context.stopService(new Intent(Cmd.ACTION_ALARM_ALERT));
		} 
		else if (Cmd.ACTION_ALARM_SNOOZE.equals(action))
		{
	    	Log.d("new-zzm", "AlarmReceiver.ACTION_ALARM_ALERT in onReceive of ACTION_ALARM_SNOOZE...");
	    	Log.f("snooze", "AlarmReceiver.onReceive: ACTION_ALARM_SNOOZE...");
			context.stopService(new Intent(Cmd.ACTION_ALARM_ALERT));
		} 
		else if (Cmd.ACTION_ALARM_TIMEOUT.equals(action))
		{
	    	Log.d("new-zzm", "AlarmReceiver.ACTION_ALARM_ALERT in onReceive of ACTION_ALARM_TIMEOUT...");
	    	Log.f("snooze", "AlarmReceiver.onReceive: ACTION_ALARM_TIMEOUT...");
			context.stopService(new Intent(Cmd.ACTION_ALARM_ALERT));
		} 
		else if (Cmd.ACTION_ALARM_ALERT.equals(action))
		{
			handleAlarmAlert(context, intent);
		}
		else if (Cmd.ACTION_ALARM_DISMISS.equals(action))
		{
			handleAlarmDismiss(context, intent);
		}
	}
	
	protected void handleAlarmDismiss(Context context, Intent intent)
	{
		Log.d("new-zzm", "AlarmReceiver : " + "handleAlarmDismiss...");
		final Alarm alarm = intent.getParcelableExtra(Cmd.Alert.EXTRA_ALARM);
		if(null != alarm)
			AlarmCore.dismissAlarm(context, alarm.id, false);
	}

	protected void handleAlarmAlert(Context context, Intent intent)
	{
		Alarm alarm = fromBytes(intent.getByteArrayExtra(Cmd.Alert.EXTRA_RAW));		
		Log.d("new-zzm", "handleAlarmAlert: start...");
		if (alarm == null)
		{
			Log.d("new-zzm", "handleAlarmAlert: no alert data");
			AlarmCore.nextAlert(context);
			return;
		}		

		//如果当前的Alarm是Snoozed，重置他的Trigger，避免再次弹出。
		AlarmCore.resetSnoozedAlarm(context, alarm.id);
		
		if (alarm.cycle.isRepeatSet())
		{
			// Enable the next alert if there is one. The above call to
			// enableAlarm will call setNextAlert so avoid calling it twice.
			Log.d("new-zzm", "handleAlarmAlert: isRepeatSet");
			AlarmCore.nextAlert(context);
		} 
		else
		{
			// Disable this alarm if it does not repeat.
			//enable alarm will call setNextAlert
			Log.d("new-zzm", "handleAlarmAlert: !!!isRepeatSet");
			AlarmCore.enableAlarm(context, alarm.id, false, false);
		}

		// Intentionally verbose: always log the alarm time to provide useful
		// information in bug reports.
		long now = System.currentTimeMillis();

		// Always verbose to track down time change problems.
		if (now > alarm.trigger + STALE_WINDOW)
		{
			Log.d("new-zzm", "Ignoring stale alarm");
			return;
		}

		// Maintain a cpu wake lock until the AlarmActivity and AlarmService
		//can pick it up.
		AlarmWakeLock.getInstance(context).acquire();

		/* Close dialogs and window shade */
		Intent closeDialogs = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
		context.sendBroadcast(closeDialogs);

		// Play the alarm alert and vibrate the device.
		Intent playAlarm = new Intent(Cmd.ACTION_ALARM_ALERT);
		playAlarm.putExtra(Cmd.Alert.EXTRA_ALARM, alarm);
		context.startService(playAlarm);

		//show the notification
		AlarmNotify2.notifyAlert(context, alarm);
	}
	
	private Alarm fromBytes(final byte[] src)
	{
		if (src != null)
		{
			final Parcel in = Parcel.obtain();
			in.unmarshall(src, 0, src.length);
			in.setDataPosition(0);
			return Alarm.CREATOR.createFromParcel(in);
		}
		return null;
	}
}
